package flst.ipiweb.struts.action.article;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import flst.ipiweb.service.ServiceArticle;
import flst.ipiweb.transverse.Article;

public class ListArticleAction extends Action{

	public ActionForward execute(
			ActionMapping mapping, 
			ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response)
			throws Exception {
			
			//On récupère la session
			HttpSession session = request.getSession();
			//Si l'administrateur n'est pas logué 
			//on le redirige sur le formulaire de login
			if(session.getAttribute("login") == null)
			{
				return mapping.findForward("login");
			}
		
			ServiceArticle sArticle = new ServiceArticle();
			int filter = 0;
			if(request.getParameter("type") != null)
			{
				filter = Integer.parseInt(request.getParameter("type"));
			}
			List<Article> listArticle;
			switch(filter)
			{
				case 1:
					int refArticle = Integer.parseInt(request.getParameter("recherche"));
					listArticle = sArticle.rechercheArticleByRef(refArticle);
					break;
				case 2:
					String descriptionCourte = request.getParameter("recherche");
					listArticle = sArticle.rechercheArticleByDescription(descriptionCourte);
					break;
				case 3:
					Double price = Double.parseDouble(request.getParameter("recherche"));
					listArticle = sArticle.rechercheArticleByPrice(price);
				default:
					listArticle = sArticle.listerArticles();
					break;
			}
			
			
			request.setAttribute("listArticle", listArticle);
			
			//Titre de la page
			request.setAttribute("pageTitle", "article.list.title");
			HashMap<String, String> onglet = new HashMap<String, String>();
			onglet.put("article", "active");
			request.setAttribute("onglet", onglet);
			
			
			return mapping.findForward("list_article");
		}

}

